package com.yc.jpyy.commonbusiness.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import com.yc.jpyy.base.service.BaseService;
import com.yc.jpyy.commonbusiness.dao.CommonbusinessDao;
import com.yc.jpyy.commonbusiness.service.CommonbusinessService;
import com.yc.jpyy.utils.Tools;
import com.yc.jpyy.utils.UtilDate;



@Service
public class CommonbusinessServiceImpl extends BaseService implements CommonbusinessService {
    
	@Autowired
	private CommonbusinessDao commonbusinessDao;


	@Override
	public List<Map<String, Object>> getToDoList(Map<String, Object> paramsMap) throws DataAccessException {
		return commonbusinessDao.getToDoList(paramsMap) ;
	}

	@Override
	public void addCalendarService(Map<String, Object> paramsMap) throws Exception {
		String dbcode = paramsMap.get("dbcode").toString();
		int num = commonbusinessDao.findCalendarByYearDao(paramsMap);
	    if(num>0) {
	    	 throw new Exception("有此年份数据");
	    }
	    int year = Integer.valueOf(paramsMap.get("year").toString());
		//获取某年的第一天
	    Calendar calendar = Calendar.getInstance();
	    calendar.clear();
	    calendar.set(Calendar.YEAR,year);
	    Date currYearFirst = calendar.getTime();
	    calendar.roll(Calendar.DAY_OF_YEAR, -1);
	    Date currYearLast = calendar.getTime();
	    //获取两个日期间的所有日期
	    DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 
		List<String> dateList = UtilDate.getDays(df.format(currYearFirst),df.format(currYearLast));
		//获取当年的所有节假日（采用聚合数据的接口）
		List<String> holidayList =  Tools.getHolidayByYear(year);
		dateList.stream().forEach(string -> {
			  int islegalholiday = holidayList.contains(string)?1:0;
			  calendar.clear();
			  try {
				  calendar.setTime(df.parse(string));
			  } catch (Exception e) {
				  e.printStackTrace();
			  }
			  int weekday = calendar.get(Calendar.DAY_OF_WEEK);
			  paramsMap.put("tbyear",year);
			  paramsMap.put("tbmonth",string.substring(5, 7));
			  paramsMap.put("datetimename", string + " 00:00:00");
			  paramsMap.put("islegalholiday", islegalholiday);
			  paramsMap.put("weekname", weekday-1);
			  paramsMap.put("dbcode", dbcode);
			  commonbusinessDao.addCalendarDao(paramsMap);
		});
	}

	/*@Override
	public int addCalendarService(Map<String, Object> paramsMap) throws Exception {
		String dbcode = paramsMap.get("dbcode").toString();
	    int num = commonbusinessDao.findCalendarByYearDao(paramsMap);
	    if(num>0) {
	    	 throw new Exception("有此年份数据");
	    }
		String year = paramsMap.get("year").toString();
		String start = year + "0101";
		String end = year + "1231";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Date dBegin = null ;
		Date dEnd = null ;
		int retValue = 0 ;
		try {
			dBegin = sdf.parse(start);
			dEnd = sdf.parse(end);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		List<Date> lDate = UtilDate.findDates(dBegin, dEnd);
		//获取休息日
		Calendar c = Calendar.getInstance();
		String[] arryMonth = {"01","02","03","04","05","06","07","08","09","10","11","12"};
		String httpArg = "";
		for (int i = 0; i < arryMonth.length; i++) {
			httpArg += year + arryMonth[i] + ",";
		}
		//发送的字符串
		httpArg = httpArg.substring(0, httpArg.length() - 1);
		String jsonResult = Tools.requestCalendar(httpArg).toString().trim();//调用第三方接口
		int length = jsonResult.length();
		
		if(length==2) {
			 throw new Exception("无此年份节假日数据");
		}
		Map<String, Object> map =  Tools.JsonToMap(jsonResult);
		String tmpYearMonth = "";
		String tmpDay = "";
		List<String> listJJR = new ArrayList<String>();//定义节假日集合
		for (String key : map.keySet()) { 
			  //System.out.println("Key = " + key); 
			  tmpYearMonth = key;
			  tmpDay = GsonUtils.toJson(map.get(key));
			  Map<String,Object> mapDay = Tools.JsonToMap(tmpDay);
			   for(String keyDay : mapDay.keySet()) { 
				  String indexDay = tmpYearMonth+keyDay;
				  listJJR.add(indexDay);
				 
			  }
			 
	     } 
		List<String> all = new ArrayList<String>();//所有元素
		//数据库参数
		Map<String,Object> sqlParamsMap = new HashMap<String,Object>();
		int weekday = 0; //星期
		for (Date date : lDate) {
			  all.add(sdf.format(date));
			  c.setTime(date);
			  weekday =c.get(Calendar.DAY_OF_WEEK);
		      sqlParamsMap.put("tbyear",year);
		      sqlParamsMap.put("tbmonth",sdf.format(date).substring(4, 6));
		      sqlParamsMap.put("datetimename", sdf.format(date));
		      sqlParamsMap.put("islegalholiday", 0);
		      sqlParamsMap.put("weekname", weekday-1);
		      sqlParamsMap.put("dbcode", dbcode);
		      commonbusinessDao.addCalendarDao(sqlParamsMap);
		}
		System.out.println(all);
		System.out.println(listJJR);
		all.retainAll(listJJR);//取交集
		System.out.println("all交集"+all);
		Map<String,Object> sqlParamsMapUpdae = new HashMap<String,Object>();
		sqlParamsMapUpdae.put("all",all);
		commonbusinessDao.updateCalendarDao(sqlParamsMapUpdae);
		return retValue;
	}
*/

	@Override
	public List<Map<String, Object>> findCalendarService(Map<String, Object> paramsMap) throws DataAccessException {
		return commonbusinessDao.findCalendarDao(paramsMap);
	}


	@Override
	public int updateCalendarByDatetimenameService(Map<String, Object> paramsMap) throws DataAccessException {
		return commonbusinessDao.updateCalendarByDatetimenameDao(paramsMap);
	}


	@Override
	public int deleteCalendarService(Map<String, Object> paramsMap) throws DataAccessException {
		return commonbusinessDao.deleteCalendarDao(paramsMap);

	
	
	}


	@Override
	public Map<String, Object> selcetCitemsInfo(Map<String, Object> paramsMap)
			throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		List<Map<String,Object>> info = commonbusinessDao.selcetCitemsInfo(paramsMap);
		if(info != null && info.size() > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData", info);
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "系统无数据");
		}
		return resultMap;
	}
	
	@Override
	public Map<String, Object> selcetCalendarByDate(Map<String, Object> paramsMap)
			throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		Map<String,Object> info = commonbusinessDao.selcetCalendarByDate(paramsMap);
		if(info != null && info.size() > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData", info.get("DateTimeName"));
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "系统无数据");
		}
		return resultMap;
	}


	@Override
	public Map<String, Object> selcetOrganList(Map<String, Object> paramsMap) throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		List<Map<String,Object>> info = commonbusinessDao.selcetOrganList(paramsMap);
		if(info != null && info.size() > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData", info);
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "系统无数据");
		}
		return resultMap;
	}
	
	@Override
	public Map<String, Object> getParameterContent(Map<String, Object> paramsMap) throws DataAccessException {
		Map<String,Object> resultMap = new HashMap<>();
		Map<String,Object> info = commonbusinessDao.getParameterContent(paramsMap);
		if(info != null && info.size() > 0){
			resultMap.put("returnCode", 1);
			resultMap.put("ListData", info.get("PARAMETERCONTENT"));
			resultMap.put("returnMsg", "操作成功");
		}else{
			resultMap.put("returnCode", 0);
			resultMap.put("returnMsg", "系统无数据");
		}
		return resultMap;
	}

	
}
